package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.RoundingMode;
import java.text.NumberFormat;

/**
 * @author ZhangyiA
 * @describe:
 * @date 2021/06/26
 */

public class GeneratePr {

    public void generatePr(String src , String dest) throws IOException {
        File srcFile = new File(src);
        assert srcFile.exists() : String.format("File path %s isn't exists , please check it." , srcFile);
        File destFile = new File(dest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile)));
        String line;
        while((line = reader.readLine()) != null){
            line = line.trim();
            if(line.equals("")){
                continue;
            }
            System.out.println(line);
            if(line.charAt(0) == 'e'){
                double pr = getPr();
                line = line + " " + pr;
            }
            writer.write(line);
            writer.newLine();
        }
        writer.flush();
        writer.close();
        reader.close();
    }

    public double getPr(){
        double pr = 0;
        while(pr < 0.2){
            pr = Math.random();
        }
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(2);
        nf.setRoundingMode(RoundingMode.UP);
        return Double.parseDouble(nf.format(pr));
    }

    public static void main(String[] args) throws IOException {
        String src = "D:\\FSM_DataSet\\G-tree.txt";
        String dest = "D:\\FSM_DataSet\\test_pr.txt";
        GeneratePr gp = new GeneratePr();
        gp.generatePr(src , dest);
    }

}
